package com.spring.boot.standard.admin.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.spring.boot.standard.admin.common.utils.GsonUtil;
import com.spring.boot.standard.admin.dao.VersionsRepository;
import com.spring.boot.standard.admin.entity.Versions;
import com.spring.boot.standard.admin.service.VersionsService;

/**
 * @author qinhao
 * @since Tue Sep 07 06:24:29 UTC 2021
 */
@Service
@EnableTransactionManagement
public class VersionsServiceImpl implements VersionsService {
	
    @Autowired
    private VersionsRepository versionsRepository;

	@PersistenceContext
	EntityManager entityManager;

    @SuppressWarnings("serial")
	@Override
	public Page<Versions> findAll(Pageable pageable,Object[] args) {
		Page<Versions> result = versionsRepository.findAll(new Specification<Versions>() {
	        @Override
	        public Predicate toPredicate(Root<Versions> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//List<Predicate> list = new ArrayList<Predicate>(); 多条件搜索的写法
				if (!StringUtils.isEmpty(args[0]) && !StringUtils.isEmpty(args[1])) {
					/**多条件搜索的写法
					list.add(cb.like(root.get("versionsid").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("version").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("remark").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("content").as(String.class), "%" + args[0] + "%"));
					Predicate[] p = new Predicate[list.size()];
					return cb.or(list.toArray(p));*/
					return cb.like(root.get(args[0].toString()).as(String.class),"%" + args[1] + "%");
				}else{
					return null;
				}
	        }
	    }, pageable);
	    return result;
	}

	@Override
	public Versions findById(String versionsid) {
		return versionsRepository.findById(versionsid).get();
	}

	@Override
	public Versions findLastestVersion(){
		Query query = entityManager.createQuery("SELECT p FROM Versions p order by version desc");
		Versions versions = (Versions) query.setMaxResults(1).getSingleResult();// 仅返回一条记录
		return versions;
	}

	@Override
	public void deleteById(String versionsid) {
		versionsRepository.deleteById(versionsid);;
	}

	@Override
	public Versions save(Versions versions) {
		return versionsRepository.save(versions);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveFromList(List<Map<String,String>> list) throws Exception {
		int i=0;
		List<Versions> versionslist = new ArrayList<Versions>();
		for(Map<String,String> map:list){
			Versions versions = GsonUtil.getGson().fromJson(GsonUtil.getGson().toJson(map),Versions.class);
			versionslist.add(versions);
		}
		for(Versions versions:versionslist){
			versionsRepository.save(versions);
			i++;
		}
		return i;
	}

    
}
